<?php

/**
 * 更新wiki 白底图以及短标题
 */

error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('display_errors', 1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\wiki\TbkGoods;
use Biz\model\wiki\Wiki;
use Biz\tools\TbkUnion;

$list = [];
$page = 1;
$pageSize = 5000;
do {
    $list = Wiki::selectByWhere([
        'channel' => Wiki::CHANNEL_TAOBAO,
        'LIMIT' => [($page - 1) * $pageSize, $pageSize],
        'status[!]' => Wiki::STATUS_DEL,
        'ORDER' => ['id' => 'DESC'],
    ],
        ['id', 'channel_spu_id', 'title']
    );
    $itemIds = array_column($list, 'channel_spu_id');
    $tbkList = TbkGoods::isTbkGoods($itemIds);
    foreach ($list as $row) {
        $itemId = $row['channel_spu_id'] ?? 0;
        $wikiID = $row['id'] ?? 0;
        if ($itemId > 0 && $wikiID > 0 && $tbkList[$itemId]) {
            $itemInfo = TbkUnion::getItemInfo($itemId);
            if (!$itemInfo) {
                continue;
            }
            $title = $itemInfo['title'];
            $tbkDgOpMaterialOptionalData = TbkUnion::tbkDgOpMaterialOptional($title, 1, 10);
            if (empty($tbkDgOpMaterialOptionalData)) {
                continue;
            }
            foreach ($tbkDgOpMaterialOptionalData as $optionalData) {
                if ($optionalData['item_id'] == $itemId) {

                    Wiki::updateData([
                        'white_image' => $optionalData['white_image'],
                        'simple_title' => $optionalData['short_title'],
                    ], ['id' => $wikiID]);
                    echo '更新wiki 白底图以及短标题#wiki_id:'.$wikiID.'#淘宝商品id:'.$itemId.'#白底图:'.$optionalData['white_image'].'#短标题:'.$optionalData['short_title'].PHP_EOL;
                    break;
                }
            }
        }
    }
    echo '这是第'.$page.'批数据'.PHP_EOL;
    $page++;
} while ($list);
